package com.ruoyi.user.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.common.util.CopyEntity;
import com.param.concat.IDiscountParam;
import com.param.concat.IUpdateByDuplicateCheckCoupon;
import com.param.concat.IUpdateByRecommend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.user.mapper.MgUserMapper;
import com.ruoyi.user.domain.MgUser;
import com.ruoyi.user.service.IMgUserService;

/**
 * 用户管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-19
 */
@Service
public class MgUserServiceImpl implements IMgUserService
{
	
	RedisTemplate redisTemplate;
	
	@Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
	
    @Autowired
    private MgUserMapper mgUserMapper;

    /**
     * 查询用户管理
     *
     * @param id 用户管理主键
     * @return 用户管理
     */
    @Override
    public MgUser selectMgUserById(String id)
    {
        return mgUserMapper.selectById(id);
    }

    /**
     * 查询用户管理列表
     *
     * @param mgUser 用户管理
     * @return 用户管理
     */
    @Override
    public List<MgUser> selectMgUserList(MgUser mgUser)
    {
        return mgUserMapper.selectList(mgUser);
    }

    /**
     * 新增用户管理
     *
     * @param mgUser 用户管理
     * @return 结果
     */
    @Override
    public int insertMgUser(MgUser mgUser)
    {
        return mgUserMapper.insert(mgUser);
    }

    /**
     * 修改用户管理
     *
     * @param mgUser 用户管理
     * @return 结果
     */
    @Override
    public int updateMgUser(MgUser mgUser)
    {
        return mgUserMapper.update(mgUser);
    }

    /**
     * 批量删除用户管理
     *
     * @param ids 需要删除的用户管理主键
     * @return 结果
     */
    @Override
    public int deleteMgUserByIds(String[] ids)
    {
        return mgUserMapper.deleteByIds(ids);
    }

    /**
     * 删除用户管理信息
     *
     * @param id 用户管理主键
     * @return 结果
     */
    @Override
    public int deleteMgUserById(String id)
    {

        return mgUserMapper.deleteById(id);
    }

	@Override
	public int updateDuplicateCheckCoupon(IUpdateByDuplicateCheckCoupon user) {

		redisTemplate.delete("module-list:" + user.getId());
		
		return this.mgUserMapper.updateDuplicateCheckCoupon(user);
	}
	
	@Override
	public int updateDiscount(IDiscountParam user) {
		
		MgUser mgUser = CopyEntity.copyEntity(user, MgUser.class);
		
		redisTemplate.delete("module-list:" + user.getId());

		return this.mgUserMapper.update(mgUser);
	}
	

	@Override
	public int updateRecommend(IUpdateByRecommend recommend) {

		int id = recommend.getId();
		
		long recommendId = recommend.getRecommendedUserId().longValue();
		
		if (id == recommendId) {
			throw new IllegalArgumentException("不能推荐自己");
		}
		
		MgUser selectById = this.selectById(id);
		
		if (selectById == null) {
			throw new IllegalArgumentException("找不到此会员");
		}
		
		if (selectById.getRecommendedUserId()!= null) {
            throw new IllegalArgumentException("已存在推荐人");
		}
		
		MgUser mgUser = new MgUser();

		mgUser.setId(id);

		mgUser.setRecommendedUserId(recommendId);

		
		int update = this.mgUserMapper.update(mgUser);
		
		redisTemplate.delete("module-list:" + id);
		return update;
	}

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int updateAcademicCurrency(BigDecimal bigDecimal, int userId) {
		// TODO Auto-generated method stub
		MgUser mgUser = new MgUser();

		mgUser.setAcademicCurrency(bigDecimal);

		mgUser.setId(userId);
		
		redisTemplate.delete("module-list:" + userId);

		return this.mgUserMapper.updateAcademicCurrency(mgUser);
	}

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int updateRecharge(MgUser user) {

		user.setUpdateTime(Long.valueOf(System.currentTimeMillis() / 1000));
		redisTemplate.delete("module-list:" + user.getId());

		return this.mgUserMapper.update(user);
	}

	@Override
	public List<MgUser> getUserList(long[] ids) {
		return mgUserMapper.selectListByIds(ids);
	}

	@Override
	public <P> List<MgUser> listBySearch(P param) {
		// TODO Auto-generated method stub
		return this.mgUserMapper.listBySearch(param);
	}

	@Override
	public <VO> int updatePayStatus(VO payManager) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<MgUser> selectListById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<MgUser> selectListByIds(int[] ids) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <VO> List<VO> getOrderLogVOSById(int id, Class<VO> voClass) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <VO> int batchInsert(List<VO> data) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int batchInsert(List<MgUser> data, int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public MgUser selectById(Integer id) {
		
		return mgUserMapper.selectById(id+"");
	}

	@Override
	public List<MgUser> selectList(MgUser manuscript) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int insert(MgUser manuscript) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(MgUser manuscript) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int countRecomendNum(long recommendNumber) {
		// TODO Auto-generated method stub
		return mgUserMapper.countRecomendNum(recommendNumber);
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long maxId() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long minId() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<MgUser> selectListByRealname(String realname) {
		// TODO Auto-generated method stub
		return mgUserMapper.selectListByRealname(realname);
	}

	
}
